In [1]:
%matplotlib inline

from compare_bmp import compare_images
from PIL import Image, ImageFilter
from matplotlib.pyplot import imshow
import numpy as np

We'll start with this image:


In [2]:
# first, open the original image and hide a message in it
imgpath = 'images/original/image.bmp'

img = Image.open(imgpath)

# we'll use simple repetition as a very rudimentary ECC to try to maintain integrity
# each bit of the message will be repeated 6 times - the two LSBs of the R,G, and B values of one pixel
#imgArray = np.copy(np.asarray(img))[:1]
imgArray = list(np.asarray(img))

def set_bit(val, bitNo, bit):
    """ given a value, which bit in the value to set, and the actual bit (0 or 1) to set, return the new value """
    mask = 1 << bitNo
    val &= ~mask
    if bit:
        val |= mask
    return val

msgIndex = 0

for toChange in range(1,9):
    newImg = []
    for row in imgArray:
        newRow = []
        for pixel in row:
            newPixel = []
            for val in pixel:
                # iterate through RGB values, one at a time
                
                for i in range(1,toChange+1):
                    val = set_bit(val, i, 0)
                
                newPixel.append(val)
            newRow.append(newPixel)
        newImg.append(newRow)

    arr = np.array(newImg, np.uint8)
    im = Image.fromarray(arr)
    im.save("bitplane_demo_{}.bmp".format(toChange))

How does the image look now?